package com.zhl.media.app.app;

import android.app.Application;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.jess.arms.base.delegate.AppLifecycles;
import com.jess.arms.di.module.GlobalConfigModule;
import com.jess.arms.integration.ConfigModule;
import com.jess.arms.utils.ArmsUtils;
import com.orhanobut.hawk.Hawk;
import com.previewlibrary.ZoomMediaLoader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.squareup.leakcanary.RefWatcher;
import com.tencent.bugly.Bugly;
import com.zhl.media.app.BuildConfig;
import com.zhl.media.app.R;
import com.zhl.media.app.utils.TaskJobManager;
import com.zhl.media.app.utils.ZoomImageLoader;
import com.zhl.media.commonsdk.http.HeaderInterceptor;
import java.util.List;
import cn.jpush.android.api.JPushInterface;
public final class GlobalConfiguration implements ConfigModule {

    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.public_white, R.color.public_color_333333);
                return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });
    }

    @Override
    public void applyOptions(Context context, GlobalConfigModule.Builder builder) {
        builder.addInterceptor(new HeaderInterceptor());
        Hawk.init(context).build();
        SDKInitializer.initialize(context);
        SDKInitializer.setCoordType(CoordType.BD09LL);
        JPushInterface.setDebugMode(BuildConfig.DEBUG);
        JPushInterface.init(context);
        initBugly(context);
        ZoomMediaLoader.getInstance().init(new ZoomImageLoader());
        TaskJobManager.getInstance(context).init();
    }

    private void initBugly(Context context) {
        Bugly.init(context,BuildConfig.BUGLY_APPID,BuildConfig.DEBUG);
    }

    @Override
    public void injectAppLifecycle(Context context, List<AppLifecycles> lifecycles) {
        // 可以根据不同的逻辑添加多个实现类
        lifecycles.add(new AppLifecyclesImpl());
    }

    @Override
    public void injectActivityLifecycle(Context context, List<Application.ActivityLifecycleCallbacks> lifecycles) {

    }

    @Override
    public void injectFragmentLifecycle(Context context, List<FragmentManager.FragmentLifecycleCallbacks> lifecycles) {
        lifecycles.add(new FragmentManager.FragmentLifecycleCallbacks() {
            @Override
            public void onFragmentDestroyed(FragmentManager fm, Fragment f) {
                ((RefWatcher) ArmsUtils
                        .obtainAppComponentFromContext(f.getActivity())
                        .extras()
                        .get(RefWatcher.class.getName()))
                        .watch(f);
            }
        });
    }

}
